package com.nongcai159.mall.lib_common.utils

import android.app.Activity
import android.content.Context
import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager

import com.nongcai159.mall.lib_common.base.BaseApplication

/**
 * 屏幕适配
 *
 *
 * Created by tanbiheng on 2018/11/23.
 */

object ScreenSupport {

    // 当前屏幕的宽度
    var SCREEN_WIDTH = 0
    // 当前屏幕的高度
    var SCREEN_HEIGHT = 0
    // 当前屏幕的密度
    var SCREEN_DESTITY = 0.0f

    // 初始化屏幕高宽
    fun displayMetrics(activity: Activity?) {
        if (activity == null) {
            return
        }

        var dm: DisplayMetrics? = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(dm)

        SCREEN_WIDTH = dm!!.widthPixels
        SCREEN_HEIGHT = dm.heightPixels
        SCREEN_DESTITY = dm.density

        dm = null
    }

    /**
     * 沉浸式状态栏适配
     * @param context
     * @param vp
     */
    fun statusBarSupport(context: Context, vp: ViewGroup) {
        var topLayoutHeight = dp2px(50f)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val statusBarHeight = getStatusHeight(context)
            topLayoutHeight += statusBarHeight
            vp.layoutParams.height = topLayoutHeight
            vp.setPadding(0, statusBarHeight, 0, 0)
        } else {
            vp.layoutParams.height = topLayoutHeight
            vp.setPadding(0, 0, 0, 0)
        }
    }

    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    fun getStatusHeight(context: Context): Int {

        var statusHeight = -1
        try {
            val clazz = Class.forName("com.android.internal.R\$dimen")
            val `object` = clazz.newInstance()
            val height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(`object`).toString())
            statusHeight = context.resources.getDimensionPixelSize(height)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return statusHeight
    }

    // 隐藏虚拟导航键
    fun hideNavigation(activity: Activity) {
        if (checkDeviceHasNavigationBar(activity)) {
            val window = activity.window
            // 虚拟导航键半透明
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            // 隐藏虚拟导航键
            window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hideController nav

                    or View.SYSTEM_UI_FLAG_IMMERSIVE)
        }
    }

    /**
     * 是否有虚拟按键
     *
     * @return
     */
    fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs = context.resources
        val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass = Class.forName("android.os.SystemProperties")
            val m = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }

        return hasNavigationBar
    }

    // 显示虚拟按键状态栏
    fun showStatusNavBar(activity: Activity) {
        val attrs = activity.window.attributes
        attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
        activity.window.attributes = attrs
    }

    // 隐藏虚拟按键状态栏
    fun hideStatusNavBar(activity: Activity) {
        val attrs = activity.window.attributes
        attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
        activity.window.attributes = attrs

        MLog.d("hideStatusNavBar")
    }

    /**
     * 获得系统亮度
     *
     * @return
     */
    fun getSystemBrightness(activity: Activity): Float {
        var systemBrightness = 0f
        try {
            systemBrightness = Settings.System.getInt(activity.contentResolver, Settings.System.SCREEN_BRIGHTNESS).toFloat()
        } catch (e: Settings.SettingNotFoundException) {
            e.printStackTrace()
        }

        return systemBrightness * 1.0f / 255
    }

    /**
     * @param pxValue
     * @return
     */
    fun px2dp(pxValue: Float): Int {
        val scale = BaseApplication.INSTANCE!!.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     * @param dipValue
     * @return
     */
    fun dp2px(dipValue: Float): Int {
        val scale = BaseApplication.INSTANCE!!.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    /**
     * @param pxValue
     * @return
     */
    fun px2sp(pxValue: Float): Int {
        val fontScale = BaseApplication.INSTANCE!!.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

    /**
     * @param spValue
     * @return
     */
    fun sp2px(spValue: Float): Int {
        val fontScale = BaseApplication.INSTANCE!!.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }
}
